package org.jeecg.modules.demo.crm.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.entity.CInvoiceTask;
import org.jeecg.modules.demo.crm.entity.CSubProject;
import org.jeecg.modules.demo.crm.mapper.CInvoiceTaskMapper;
import org.jeecg.modules.demo.crm.mapper.CSubProjectMapper;
import org.jeecg.modules.demo.crm.service.ICInvoiceTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 开票登记
 * @Author: jeecg-boot
 * @Date:   2025-05-27
 * @Version: V1.0
 */
@Service
public class CInvoiceTaskServiceImpl extends ServiceImpl<CInvoiceTaskMapper, CInvoiceTask> implements ICInvoiceTaskService {

    @Autowired
    private CInvoiceTaskMapper cInvoiceTaskMapper;

    @Autowired
    private CSubProjectMapper cSubProjectMapper;

    @Override
    public void addInvoice(CInvoiceTask cInvoiceTask) {
        CSubProject cSubProject = cSubProjectMapper.selectById(cInvoiceTask.getSubProjectId());
        if(cSubProject==null){
            throw new RuntimeException("子项目不存在");
        }
        if (!cSubProject.getProjectId().equals(cInvoiceTask.getProjectId())){
            throw new RuntimeException("子项目与主项目不匹配");
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(!cSubProject.getUserId().equals(sysUser.getId())){
            throw new RuntimeException("项目无权限申请");
        }
        cInvoiceTask.setStatus("待审核");
        cInvoiceTaskMapper.insert(cInvoiceTask);
    }

    @Override
    public void audit(CInvoiceTask cInvoiceTask) {
        CInvoiceTask cInvoiceTaskVo = cInvoiceTaskMapper.selectById(cInvoiceTask.getId());
        cInvoiceTaskVo.setStatus(cInvoiceTask.getStatus());
        cInvoiceTaskVo.setAuditResults(cInvoiceTask.getAuditResults());
        cInvoiceTaskVo.setAuditFile(cInvoiceTask.getAuditFile());
        cInvoiceTaskMapper.updateById(cInvoiceTaskVo);

    }
}
